package com.neusoft.javafxdemo2.controller;

import com.neusoft.javafxdemo2.SupervisorLogin;
import com.neusoft.javafxdemo2.common.AlertUtils;
import com.neusoft.javafxdemo2.entity.Supervisor;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.Map;

public class SupervisorLoginViewController {
    @FXML
    private TextField txtTelId;
    @FXML
    private PasswordField txtPassword;
    public static Supervisor currentLoginSupervisor;
    public void login(){
        String telId = txtTelId.getText();
        String password = txtPassword.getText();
        System.out.println("telId:" + telId +",password:" + password);
        //处理登录逻辑，读取supervisor.txt文件中的数据，对比用户输入的telId是否存在，如果存在，密码是否正确
        try {
            FileInputStream fis = new FileInputStream("supervisor.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Map<String, Supervisor> map = (Map<String, Supervisor>)ois.readObject();
            System.out.println(map);
            //判断手机号是不是存在
            if(!map.containsKey(telId)){
                AlertUtils.showDialog("用户登录","手机号不存在");
                return;
            }
            //判断密码是否正确
            Supervisor s = map.get(telId);
            if(!s.getPassword().equals(password)){
                AlertUtils.showDialog("用户登录","密码不正确");
                return;
            }
            //跳转到主界面
            AlertUtils.switchToView(txtTelId,"SupervisorMainView.fxml",1200,1000);
            //登录成功以后，将登录的公众监督员实例设置为一个静态的属性，以便其他页面进行访问
            currentLoginSupervisor = s;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public void register() throws IOException {
        AlertUtils.switchToView(txtTelId,"SupervisorRegisterView.fxml");
    }
}
